package com.hytc.mymall.db;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.hytc.mymall.bean.AccountBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/1.
 */


public class AccountBD
{
    public static final String lock = "访问";
    private DBOpenHelper openHelper;

    public AccountBD(final Context context) {
        this.openHelper = DBOpenHelper.getInstance(context);
    }

    public void create() {
        final SQLiteDatabase writableDatabase = this.openHelper.getWritableDatabase();
        writableDatabase.execSQL("CREATE TABLE IF NOT EXISTS accountinfo (id integer primary key autoincrement, account varchar, style integer)");
        writableDatabase.close();
    }

    public void delete(final String s, final int n) {
        final SQLiteDatabase writableDatabase = this.openHelper.getWritableDatabase();
        writableDatabase.execSQL("delete from accountinfo where account=? AND style=? ", new Object[] { s, new StringBuilder(String.valueOf(n)).toString() });
        writableDatabase.close();
    }

    public void drop() {
        final SQLiteDatabase writableDatabase = this.openHelper.getWritableDatabase();
        writableDatabase.execSQL("DROP TABLE IF EXISTS accountinfo");
        writableDatabase.close();
    }

    public void insert(final String s, final int n) {
        final SQLiteDatabase writableDatabase = this.openHelper.getWritableDatabase();
        writableDatabase.beginTransaction();
        while (true) {
            try {
                writableDatabase.execSQL("insert into accountinfo(account ,style) values(?,?)", new Object[] { s, n });
                writableDatabase.setTransactionSuccessful();
                writableDatabase.endTransaction();
                writableDatabase.close();
            }
            catch (SQLException ex) {
                ex.printStackTrace();
                writableDatabase.endTransaction();
                continue;
            }
            finally {
                writableDatabase.endTransaction();
            }
            break;
        }
    }

    public boolean isAccountExit(final String s, final int n) {
        boolean b = false;
        final SQLiteDatabase readableDatabase = this.openHelper.getReadableDatabase();
        Cursor rawQuery = null;
        try {
            rawQuery = readableDatabase.rawQuery("select account , style from accountinfo where account = ? and style = ?", new String[] { s, new StringBuilder(String.valueOf(n)).toString() });
            while (rawQuery.moveToNext()) {
                b = true;
            }
            rawQuery.close();
            readableDatabase.close();
            return b;
        }
        catch (SQLException ex) {
            if (rawQuery != null) {
                rawQuery.close();
            }
            if (readableDatabase != null) {
                readableDatabase.close();
            }
            ex.printStackTrace();
            return b;
        }
    }

    public List<AccountBean> queryAll() {
        List<AccountBean> list=new ArrayList<>();
        SQLiteDatabase db = openHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from accountinfo", null);
        while (cursor.moveToNext()) {
            String accout = cursor.getString(0);
            int style = cursor.getInt(2);
            AccountBean accountBean=new AccountBean(accout,style);
            list.add(accountBean);
        }
        cursor.close();
        db.close();
       return list;
    }
}
